package com.wzcl.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.wzcl.app.dao.model.Activity;
import com.wzcl.app.dao.model.VideoResources;
import com.wzcl.app.dao.repository.VideoResourcesRepository;
import com.wzcl.app.model.common.BusinessException;
import com.wzcl.app.model.common.CodeMsg;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.dto.VideoResourceDto;
import com.wzcl.app.service.VideoResourceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Log4j2
@Service
public class VideoResourceServiceImpl implements VideoResourceService {

    @Autowired
    private VideoResourcesRepository repository;

    @Override
    public VideoResources addVideoResource(VideoResourceDto videoResourceDto) {
        VideoResources videoResources = new VideoResources();
        BeanUtils.copyProperties(videoResourceDto, videoResources);
        return repository.save(videoResources);
    }

    @Override
    public VideoResources delVideoResourceById(Integer id) {
        //校验获得
        VideoResources resource = getAndCheckById(id);
        //删除
        log.info("VideoResources:{} Physical deletion", JSON.toJSONString(resource));
        repository.delete(resource);
        return resource;
    }

    @Override
    public VideoResources getVideoResourceById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public VideoResources getAndCheckById(Integer id) {
        if (id == null) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少素材Id");
        }
        VideoResources resource = getVideoResourceById(id);
        if (resource == null) {
            throw new BusinessException(CodeMsg.VIDEO_RESOURCE_NOT_EXIST);
        }
        return resource;
    }

    @Override
    public List<VideoResources> getVideoResourceList(VideoResourceDto videoResourceDto) {
        return repository.findAll(getSpecification(videoResourceDto), videoResourceDto.getSort());
    }

    @Override
    public MyPage<VideoResources> getVideoResourcePage(VideoResourceDto videoResourceDto) {
        Page<VideoResources> all = repository.findAll(getSpecification(videoResourceDto), videoResourceDto.getPageable());
        ArrayList<VideoResources> list = new ArrayList<>(all.getContent());
        Collections.reverse(list);
        MyPage<VideoResources> myPage = new MyPage<>();
        myPage.setContent(list);
        myPage.setTotalElements(all.getTotalElements());
        return myPage;
    }

    private Specification<VideoResources> getSpecification(VideoResourceDto videoResourceDto) {
        return new Specification<VideoResources>() {
            @Override
            public Predicate toPredicate(Root<VideoResources> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if (videoResourceDto.getResourceId() != null) {
                    predicates.add(criteriaBuilder.equal(root.get("resourceId"), videoResourceDto.getResourceId()));
                }
                if (videoResourceDto.getResourceType() != null) {
                    predicates.add(criteriaBuilder.equal(root.get("resourceType"), videoResourceDto.getResourceType()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
    }

}
